package com.huqu.beans.nghenghiep;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;

import java_cup.internal_error;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;

import com.huqu.beans.sessionWeb.SessionWeb;
import com.huqu.dao.N100Dao;
import com.huqu.dao.N200Dao;
import com.huqu.dao.N300Dao;
import com.huqu.entities.N100;
import com.huqu.entities.N200;
import com.huqu.utils.ApplicationConstant;
import com.huqu.utils.ApplicationHelper;

@ManagedBean(name="nghenghiepBean")
@ViewScoped
public class NgheNghiepBean implements Serializable{
	private static final long serialVersionUID = 6858512253433003055L;
	
	@ManagedProperty("#{sessionWeb}")
	private SessionWeb sessionWeb;
	private String vLang="VN";
	
	private List<N100> listN100= new ArrayList<N100>(0);
	private List<N200> listN200= new ArrayList<N200>(0);
	
	// list danh sach các nghề nghiệp
	HashMap<N100, List<N200>> listJobs=new HashMap<N100, List<N200>>();
	HashMap<Integer, List<N200>> listJobs2=new HashMap<Integer, List<N200>>();

// ===================================================================================================================
// =====================================================FUNCTIONS=====================================================
	@PostConstruct
	public void init(){
		try {
			System.out.println("====== NgheNghiepBean =======");
			
			if(ApplicationHelper.getRequest().getParameter(ApplicationConstant.PRETTY_PARA_LANG)!=null){
				vLang=ApplicationHelper.getRequest().getParameter(ApplicationConstant.PRETTY_PARA_LANG).toString().toUpperCase();
				
				if(vLang.trim().toUpperCase().equals("VN")){
					if(sessionWeb.locale.equals(Locale.US)){
						sessionWeb.setLocale(new Locale("vi", "VN"));
						FacesContext.getCurrentInstance()
		    			.getViewRoot().setLocale(new Locale("vi", "VN"));
						RequestContext.getCurrentInstance().execute( "location.reload();");
					}
				}else if(vLang.trim().toUpperCase().equals("EN")){
					if(sessionWeb.locale.equals(new Locale("vi", "VN"))){
						sessionWeb.setLocale(Locale.US);
						FacesContext.getCurrentInstance()
		    			.getViewRoot().setLocale(Locale.US);
						RequestContext.getCurrentInstance().execute( "location.reload();");
					}
				}
			}else{
				System.out.println("---language khong xac dinh ");
			}
			
			// Load danh sách các thể loại
			getListOfTabN100();
			// Load danh sách các bài viết
			getListOfTabN200();
			
			if(listN100.size()>0){
				for(N100 n100:listN100){
					List<N200> list=new ArrayList<N200>();
					for(int a=0;a<listN200.size();a++){
						if(listN200.get(a).getIdtheloai()==n100.getIdtheloai()){
							list.add(listN200.get(a));
							listN200.remove(a);
							a--;
						}else{
							break;
						}
					}
					listJobs2.put(n100.getIdtheloai(), list);
					list=new ArrayList<N200>();
				}
			}
			
			
			/*System.out.println("===== danh sach hashmap ====");
			
			for(N100 n100:listN100){
				System.out.println("========== ket qua "+n100.getTentheloai());
				for(N200 n200:listJobs.get(n100)){
					System.out.println("===============  "+n200.getIdbaiviet()+" : "+n200.getTieude());
				}
			}*/
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// load danh sách các thể loại (ngành nghề) của mục nghề nghiệp
	public void getListOfTabN100(){
		try {
			N100Dao n100Dao = (N100Dao) ApplicationHelper.findBean(ApplicationConstant.REPOSITORY_NAME_N100);
			System.out.println("---ListOfTabN100( "+"1, 1, "+ vLang+" )");
			listN100=n100Dao.ListOfTabN100(1, 1, vLang);
			System.out.println("ListN100.size()= "+listN100.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// load danh sách các bài viết mục nghề nghiệp
	public void getListOfTabN200(){
		try {
			N200Dao n200Dao = (N200Dao) ApplicationHelper.findBean(ApplicationConstant.REPOSITORY_NAME_N200);
			System.out.println("---ListOfTabN200( "+"1, 10, "+ vLang+" )");
			listN200=n200Dao.ListOfTabN200(1, 10, vLang);
			System.out.println("ListN200.size()= "+listN200.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
// ===================================================================================================================
// ===================================================== GET-SET =====================================================

	public HashMap<N100, List<N200>> getListJobs() {
		return listJobs;
	}

	public void setListJobs(HashMap<N100, List<N200>> listJobs) {
		this.listJobs = listJobs;
	}

	public String getvLang() {
		return vLang;
	}

	public void setvLang(String vLang) {
		this.vLang = vLang;
	}

	public SessionWeb getSessionWeb() {
		return sessionWeb;
	}

	public void setSessionWeb(SessionWeb sessionWeb) {
		this.sessionWeb = sessionWeb;
	}

	public List<N100> getListN100() {
		return listN100;
	}

	public void setListN100(List<N100> listN100) {
		this.listN100 = listN100;
	}

	public List<N200> getListN200() {
		return listN200;
	}

	public void setListN200(List<N200> listN200) {
		this.listN200 = listN200;
	}

	public HashMap<Integer, List<N200>> getListJobs2() {
		return listJobs2;
	}

	public void setListJobs2(HashMap<Integer, List<N200>> listJobs2) {
		this.listJobs2 = listJobs2;
	}
	
}


